package n.d.l.svn.service;

import java.io.File;
import java.util.List;

import n.d.l.svn.bean.Project;
import n.d.l.svn.bean.ResponseBean;
import n.d.l.svn.bean.SvnCommand;
import n.d.l.svn.mapper.project.ProjectMapper;
import n.d.l.utils.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly=true)
public class ProjectService {

	@Autowired
	private ProjectMapper projectMapper;
	
	@Transactional(readOnly=false)
	public ResponseBean edit(Project project) {
		ResponseBean resp = new ResponseBean();
		List<String> lines = Utils.doCommand(SvnCommand.INFO, new File(project.getSvnDir()));
		if(lines.isEmpty() || lines.get(0).endsWith("is not a working copy")){
			resp.setSuccess(false);
			resp.setMsg("无效的svn目录");
			return resp;
		}
		String relativeUrl = lines.get(3);
		relativeUrl = relativeUrl.substring(relativeUrl.indexOf("/")+1)+"/";
		project.setRelativeUrl(relativeUrl);
		int result = 0;
		if(project.getId()!=0){
			result = projectMapper.update(project);
		}else{
			result = projectMapper.save(project);
		}
		if(result == 0){
			resp.setSuccess(false);
			resp.setMsg("项目编辑失败！");
		}
		return resp;
	}

	public List<Project> findList(Project project) {
		return projectMapper.findList(project);
	}

	@Transactional(readOnly=false)
	public int delById(int id) {
		return projectMapper.delById(id);
	}

	public Project getById(int id) {
		return projectMapper.getById(id);
	}

	@Transactional(readOnly=false)
	public int updateIgnore(Project project) {
		return projectMapper.updateIgnore(project);
	}
	
}
